package com.rimi.bbs.controller;

import com.rimi.bbs.bean.JsonResult;
import com.rimi.bbs.service.MailService;
import com.wf.captcha.SpecCaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 *  邮件服务
 */
@RestController
@RequestMapping("/mail")
@SessionAttributes("captcha")
public class MailController {

    @Autowired
    private MailService mailService;

    @GetMapping("/vercode/to/{email}")
    public Object send(@PathVariable String email, Model model){
        if(StringUtils.isEmpty(email)){
           return JsonResult.error("邮箱地址不能为空");
        }
        String code = new SpecCaptcha().text();
        model.addAttribute("captcha",code);
        boolean b = mailService.sendSimpleMail(email, "RIMI论坛验证码", "验证码:"+code);
        if(!b){
            return JsonResult.error("发送邮件失败,请检查后重试");
        }
        return JsonResult.ok("邮件发送成功");
    }

}
